<template>
	<view>
		<nav-bar home :bgColor="['#CCCCCC','#CCCCCC']" backState="2000" fontColor="#333333" title="羊群羊只管理"></nav-bar>
		<v-tabs v-model="activeTab" @change="changeTab" :scroll="false" :bold="true" :tabs="tabs1"
			field="name"></v-tabs>

		<view class="content">
			<uni-search-bar placeholder="请扫描耳标" v-model="sheepNo" @confirm="search"></uni-search-bar>
			<button type="primary" class="add-btn" @click="handleAdd">新增</button>
			<uni-table ref="table" :loading="loading" border stripe type="selection" emptyText="暂无更多数据" @selection-change="handleSelectionChange">
			  <uni-tr>
			    <uni-th width="80" align="center">品质</uni-th>
			    <uni-th width="80" align="center">类型</uni-th>
			    <uni-th width="80" align="center">状态</uni-th>
			    <uni-th width="80" align="center">体重</uni-th>
				<uni-th width="80" align="center">编号</uni-th>
				<uni-th width="80" align="center">养殖</uni-th>
			 </uni-tr>
			  <uni-tr v-for="(item, index) in dataList" :key="index">
			    <uni-td align="center">{{ item.qualityEval }}</uni-td>
			    <uni-td align="center">{{ item.sheepType }}</uni-td>
			    <uni-td align="center">{{ item.status }}</uni-td>
			    <uni-td align="center">{{ item.weight }}</uni-td>
				 <uni-td align="center">{{ item.sheepNo }}</uni-td>
				  <uni-td align="center">{{ item.farmType }}</uni-td> 
			  </uni-tr>
			</uni-table>
			<view style="display: flex;border-bottom: 1px solid #e3e3e3;">
				<view class="tag1" v-if="activeTab == 4"><button type="primary" class="add-btn" @click="handleSex()">配种</button></view>
				<view class="tag1"><button type="primary" class="add-btn" @click="handleVinccine()">疫苗</button></view>
				<view class="tag1" v-if="activeTab == 4"><button type="primary" class="add-btn" @click="handleLabory()">产羔</button></view>
				<view class="tag1" v-if="activeTab == 3"><button type="primary" class="add-btn" @click="handleSold()">出栏</button></view>
				<view class="tag1"><button type="primary" class="add-btn" @click="handleTransit()">转让</button></view>
				
				<view class="tag1"><button type="primary" class="add-btn" @click="handleSlauder()">屠宰</button></view>
				<view class="tag1" v-if="activeTab != 4"><button type="primary" class="add-btn" @click="handleCastrate()">阉割</button></view>
				<view class="tag1"><button type="primary" class="add-btn" @click="handleMedicine()">用药</button></view>
				<view class="tag1"><button type="primary" class="add-btn" @click="handleSetHead()">头羊</button></view>
				<view class="tag1"><button type="primary" class="add-btn" @click="jiuxindai()">查看</button></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs1: [{
						name: '商品羊',
						value: 1
					},
					{
						name: '种公羊',
						value: 2
					},
					/* {
						name: '羊羔',
						value: 3
					},
					{
						name: '可出栏',
						value: 4
					}, */
					{
						name: '种母羊',
						value: 3
					}
				],
				activeTab: 0,
				loading:null,
				dataList: [],
				ids: [],
				selectionList: []
			}
		},
		onLoad() {
			//this.loadData();
			this.changeTab(0) ;
		
			// this.finCustomerCreditApplication();
			// this.myLoanProductAllList();
		},
		methods: {
			changeTab(e) {
				this.activeTab = e
			
				console.log(e)
				if (e == 0) {
					const typeList = '商品羊'
					this.findSheepInforList(typeList,this.supplierNo)
				} else if (e == 1) {
					const typeList = '种公羊'
					this.findSheepInforList(typeList)
				}else {
				//	const proveStatusList = ['proved']
					const typeList = '种母羊'
					this.findSheepInforList(typeList)

				}
				/* else if (e == 2) {
					const typeList = '羊羔'
					this.findSheepInforList(typeList)
				} else if (e == 3) {
					//const proveStatusList = ['rejected']
					const typeList = '可出栏'
					this.findSheepInforStatusList(typeList)
				} */ 
			},
			handleSelectionChange(e) {
			  this.selectionList = e.detail.index.map(i => this.dataList[i])
			},
			search(e){
					
				
					
					let opt = {
						url: 'basic/gzyySheepInfor/search',
						method: 'POST'
					};
					let params = {
					
						sheepNo:  e.value
					}
					this.$http.httpTokenRequest(opt, params).then(res => {
						let rangeList = res.data.data
						for (let i in rangeList) {
						
							// if(rangeList[i].creditLineUpto.toString().length > 7){
							// 	rangeList[i].creditLineUpto = rangeList[i].creditLineUpto.toString().slice(0,4)
							// }
							// else{
							// 	rangeList[i].creditLineUpto = rangeList[i].creditLineUpto.toString().slice(0,3)
							// }
						}
						this.dataList = rangeList
					})
				
			},
			handleAdd() {
			  uni.navigateTo({
			    url: `/pages/resource/addSheep`
			  });
			},
			handleVinccine(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  	
			 // alert(ids);
			  uni.navigateTo({
			    url: `/pages/resource/addVinccine?sheepId=` + ids
			  });
			},
			
			handleSetHead(){
				const ids = this.selectionList.map(item => item.id).join(',')
				let opt = {
					url:'basic/gzyySheepInfor/setHead',
					method:'POST'
				};
				
				let params = {	
					
					ids:  ids
				};
				
				this.$http.httpTokenRequest(opt,params).then(res => {
					if(res.data.code == 200){
						uni.showToast({
							title: res.data.msg,
							icon: 'success',
							duration: 1500
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},1500)
					}
					else{
						uni.showToast({
							title: res.data.msg,
							icon: 'error',
							duration: 1500
						});
					}
				})
			},
			handleLabory(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  uni.navigateTo({
			    url: `/pages/resource/addLabory?sheepId=` + ids
			  });
			},
			handleSex(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  uni.navigateTo({
			    url: `/pages/resource/addSex?sheepId=` + ids
			  });
			},
			handleSold(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  uni.navigateTo({
			    url: `/pages/resource/addSold?sheepId=` + ids
			  });
			},			
			handleCastrate(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  uni.navigateTo({
			    url: `/pages/resource/addCastrate?sheepId=` + ids
			  });
			},
			handleMedicine(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  uni.navigateTo({
			    url: `/pages/resource/addMedicine?sheepId=` + ids
			  });
			},
			handleSlauder(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  uni.navigateTo({
			    url: `/pages/resource/addSlauder?sheepId=` + ids
			  });
			},
			
			handleTransit(e) {
			  const ids = this.selectionList.map(item => item.id).join(',')
			  uni.navigateTo({
			    url: `/pages/resource/addTransit?sheepId=` + ids
			  });
			},
		
			finCustomerCreditApplication(proveStatusList) {
				let opt = {
					url: 'baoli/finCustomerCreditApplication/search',
					method: 'POST'
				};
				let params = {
					customerNo: uni.getStorageSync('userInfo').customerId,
					proveStatusList: proveStatusList
				}
				this.$http.httpTokenRequest(opt, params).then(res => {
					let rangeList = res.data.data
					for (let i in rangeList) {
						rangeList[i].finLoanProduct.creditLineUpto = rangeList[i].finLoanProduct.creditLineUpto /
							10000
					}
					this.dataList = rangeList
					console.log(this.dataList)
				})
			},
			
			findSheepInforStatusList(status) {
				let opt = {
					url: 'basic/gzyySheepInfor/search',
					method: 'POST'
				};
				let params = {
					sheepBelongTo: uni.getStorageSync('userInfo').customerId,
					status: status
				}
				this.$http.httpTokenRequest(opt, params).then(res => {
					let rangeList = res.data.data
					for (let i in rangeList) {
						rangeList[i].finLoanProduct.creditLineUpto = rangeList[i].finLoanProduct.creditLineUpto /
							10000
					}
					this.dataList = rangeList
					console.log(this.dataList)
				})
			},
			findSheepInforList(typelist) {
				let opt = {
					url: 'basic/gzyySheepInfor/search',
					method: 'POST'
				};
				let params = {
					sheepBelongTo: uni.getStorageSync('userInfo').customerId,
					sheepType: typelist
				}
				this.$http.httpTokenRequest(opt, params).then(res => {
					let rangeList = res.data.data
					for (let i in rangeList) {
					//	rangeList[i].finLoanProduct.creditLineUpto = rangeList[i].finLoanProduct.creditLineUpto /
					//		10000
					}
					this.dataList = rangeList
					console.log(this.dataList)
				})
			},
			loadData() {
				let opt = {
					url: 'basic/gzyySheepInfor/search',
					method: 'POST'
				};
				let params = {
					sheepBelongTo: uni.getStorageSync('userInfo').customerId,
					
				}
				this.$http.httpTokenRequest(opt, params).then(res => {
					let rangeList = res.data.data
					for (let i in rangeList) {
						// rangeList[i].creditLineUpto = rangeList[i].creditLineUpto / 10000
						// if(rangeList[i].creditLineUpto.toString().length > 7){
						// 	rangeList[i].creditLineUpto = rangeList[i].creditLineUpto.toString().slice(0,4)
						// }
						// else{
						// 	rangeList[i].creditLineUpto = rangeList[i].creditLineUpto.toString().slice(0,3)
						// }
					}
					this.dataList = rangeList
				})
			},
			mdclick() {
				uni.navigateTo({
					url: '/pages/business-hall/business-hall'
				})
			},
			jiuxindai() {
				let e = this.selectionList[0]
			//	if (this.activeTab != 0) {
					let limitData = JSON.stringify(e)
				
					if (e.status == 'proved') {
						uni.navigateTo({
						//	url: '/pages/limit/financing?id=' + limitData
						url: '/pages/resource/sheepView?id=' + limitData
						})
					} else if (e.status == 'submitted') {
						if (e.productNo == '1') {
							uni.navigateTo({
								//url: '/pages/login/jdLogin?type=productApply'
								url: '/pages/resource/sheepView?id=' + limitData
							})
						}
					} else if (e.status == 'rejected') {

					} else {
						
						uni.navigateTo({
						//	url: '/pages/product/products?id=' + limitData
							url: '/pages/resource/sheepView?id=' + limitData
						})
					}
			//	}

			},
			complete(e) {
				console.log('执行完后+', e)
			},
		}
	}
</script>

<style>
	page {
		background: #f9f9f9;
	}

	.content {
		width: 95%;
		margin: 0 auto;
	}

	.productList {
		height: 240rpx;
		width: 100%;
		background-image: linear-gradient(#ffffff, #f9ffff, #f7ffff);
		border-radius: 15rpx;
		margin-top: 20rpx;
	}

	.tag {
		color: dodgerblue;
		width: 120rpx;
		height: 50rpx;
		text-align: center;
		border: 1px solid #efefef;
		border-radius: 25px;
		font-size: 25rpx;
		line-height: 50rpx;
		background: #b5e3ff;
		position: absolute;
		right: 5%;
		margin-top: 30rpx;

	}

	.tag1 {
		color: #39ed02;
		width: 120rpx;
		height: 50rpx;
		text-align: center;
		border: 1px solid #39ed02;
		border-radius: 25px;
		font-size: 25rpx;
		line-height: 50rpx;
		margin: 20rpx 10rpx;
	}

	.tag2 {
		color: #f4e10b;
		width: 120rpx;
		height: 50rpx;
		text-align: center;
		border: 1px solid #f4e10b;
		border-radius: 25px;
		font-size: 25rpx;
		line-height: 50rpx;
		margin: 20rpx 10rpx;
	}

	.numberBox {
		height: 170rpx;
		width: 220rpx;
		text-align: center;
	}

	.colorBar {
		height: 50rpx;
		width: 3rpx;
		background: #9d9d9d;
		margin-top: 60rpx;
	}
</style>